package com.csdj.common.utils;


import javax.crypto.Cipher;
import java.io.ByteArrayOutputStream;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

public class RSAUtils {

	public static final String CHARSET = "UTF-8";

	public static final String KEY_ALGORITHM = "RSA";
	public static final String SIGNATURE_ALGORITHM = "MD5withRSA";
	/**
	 * RSA最大加密明文大小
	 */
	private static final int MAX_ENCRYPT_BLOCK = 117;

	/**
	 * RSA最大解密密文大小
	 */
	private static final int MAX_DECRYPT_BLOCK = 128;
	private static final String PUBLIC_KEY = "RSAPublicKey";
	private static final String PRIVATE_KEY = "RSAPrivateKey";
	private static final String PUBKEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCf84cAmjnz6RRa97+GKlHFzqimMRlCDbY91glbKku5F8QXx0uiFYPElozAcOcxUz8Z/BttWNmxV5yAWxMvPsLxdAvDq8uiqHLrmXa38PWspChkm2TsHU4ct2z4RjsijIwfUy8Se2tVVkp93X/Tv1s1nrv9oYmCewrNIEOQ9LCs9wIDAQAB";
	private static final String PRIKEY = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAJ/zhwCaOfPpFFr3v4YqUcXOqKYxGUINtj3WCVsqS7kXxBfHS6IVg8SWjMBw5zFTPxn8G21Y2bFXnIBbEy8+wvF0C8Ory6KocuuZdrfw9aykKGSbZOwdThy3bPhGOyKMjB9TLxJ7a1VWSn3df9O/WzWeu/2hiYJ7Cs0gQ5D0sKz3AgMBAAECgYBkS4mNgSdxn+cZm1vGvuaDiDAL9aGG3540a+918LGGoDZfSqJMQf1b2aLsUF9HeJGKs6BnEl1+bBdADNuctlf5lzBH2E+oY2acasM4ooYB0P/lLYuPr2mUVWdQS6iUtzQmGkKMx4HSJBYVuZlfTIAdtysa5FdJ0OIJgQDbqIU2MQJBAPtPRZETfHiOoDGhGGwq8DXudzXjEW2usmplonR19xIRTBuyjWa4QlPZP2u8E2NiIn4rVveYzRMx4dYE0DJAnRUCQQCi78GEg4q5/wiA3glvsccwZ8OGRTt8/jG61YmbPjvjexp+VomgzMek6NEBIFSWb3LX8o4sI2EeKhBP+WA0TBzbAkAnN3igqgzLd15SQFRiVNUFZYAe3Z9ToWZgKhoO2HXE0QQyckuUkv5uvUEjW8cexCqjy7mXi7W84BTg5AFlRiX9AkBAqHIb+vNXQfyG3xXRjDPmYpb1to2X2Wu0n76eq4CqkhR+ZCrzc7AaIgWms8S9efaR+YAvxc5pYPneMrDZsAaXAkAZf8J3VYObso0MGUKMPD1sKTCxWhsVhpb59JV81asHWQEj3BeBHLPoKnZ4jZcH6REdhngmpR6EsC5iBzJbrFgZ";

	private static final Map<String, Object> keyMap = new HashMap<>();


	/**
	 * 取得私钥
	 *
	 * @return
	 * @throws Exception
	 */
	public static String getPrivateKey() {
//		Key key = (Key) keyMap.get(PRIVATE_KEY);
//		return encryptBASE64(key.getEncoded());
		try {
			return keyMap.get(PRIVATE_KEY).toString();
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

	/**
	 * 取得公钥
	 *
	 * @return
	 * @throws Exception
	 */
	public static String getPublicKey() {
		try {
			return keyMap.get(PUBLIC_KEY).toString();
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
//		Key key = (Key) keyMap.get(PUBLIC_KEY);
//		return encryptBASE64(key.getEncoded());
	}

	/**
	 * 初始密钥
	 *
	 * @return
	 * @throws Exception
	 */
	public static Map<String, Object> initKey() {
		KeyPairGenerator keyPairGen;
		try {
			keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);

			keyPairGen.initialize(1024);
			KeyPair keyPair = keyPairGen.generateKeyPair();
			// 公钥
			PublicKey publicKey = keyPair.getPublic();
			// 私钥
			PrivateKey privateKey = keyPair.getPrivate();
			System.out.println("初始化公钥: "+ encryptBASE64(publicKey.getEncoded()));
			System.out.println("初始化私钥: "+ encryptBASE64(privateKey.getEncoded()));

			keyMap.put(PUBLIC_KEY, encryptBASE64(publicKey.getEncoded()));
			keyMap.put(PRIVATE_KEY, encryptBASE64(privateKey.getEncoded()));

			return keyMap;
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			keyMap.put(PUBLIC_KEY, PUBKEY);
			keyMap.put(PRIVATE_KEY, PRIKEY);
			return keyMap;
		}
	}

	/**
	 * Base64加密
	 *
	 * @param data
	 * @return
	 */
	public static String encryptBASE64(byte[] data) {
		return new String(Base64.getEncoder().encode(data));
	}

	/**
	 * base64解密
	 *
	 * @param data
	 * @return
	 */
	public static byte[] decryptBASE64(String data) {
		return Base64.getDecoder().decode(data);
	}

	/**
	 * 公钥加密
	 * @param data
	 * @return
	 */
	public static String encryptByPublic(String data) {
		try {
			return encrypt(data, getPublicKey(getPublicKey()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 公钥加密
	 * @param data
	 * @param publicKey
	 * @return
	 */
	public static String encryptByPublic(String data, String publicKey) {
		try {
			return encrypt(data, getPublicKey(publicKey));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}


	/**
	 * 私钥解密
	 * @param data
	 * @return
	 */
	public static String decryptByPrivate(String data) {
		try {
			return decrypt(data, getPrivateKey(getPrivateKey()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 私钥解密
	 * @param data
	 * @param privateKey
	 * @return
	 */
	public static String decryptByPrivate(String data, String privateKey) {
		try {
			return decrypt(data, getPrivateKey(privateKey));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}



	/**
	 * 获取私钥
	 *
	 * @param privateKey 私钥字符串
	 * @return
	 */
	public static PrivateKey getPrivateKey(String privateKey) throws Exception {
		KeyFactory keyFactory = KeyFactory.getInstance("RSA");
		byte[] decodedKey = Base64.getDecoder().decode(privateKey.getBytes());
		PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(decodedKey);
		return keyFactory.generatePrivate(keySpec);
	}

	/**
	 * 获取公钥
	 *
	 * @param publicKey 公钥字符串
	 * @return
	 */
	public static PublicKey getPublicKey(String publicKey) throws Exception {
		KeyFactory keyFactory = KeyFactory.getInstance("RSA");
		byte[] decodedKey = Base64.getDecoder().decode(publicKey.getBytes());
		X509EncodedKeySpec keySpec = new X509EncodedKeySpec(decodedKey);
		return keyFactory.generatePublic(keySpec);
	}

	/**
	 * RSA加密
	 *
	 * @param data 待加密数据
	 * @param publicKey 公钥
	 * @return
	 */
	public static String encrypt(String data, PublicKey publicKey) throws Exception {
		Cipher cipher = Cipher.getInstance("RSA");
		cipher.init(Cipher.ENCRYPT_MODE, publicKey);
		int inputLen = data.getBytes().length;
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		int offset = 0;
		byte[] cache;
		int i = 0;
		// 对数据分段加密
		while (inputLen - offset > 0) {
			if (inputLen - offset > MAX_ENCRYPT_BLOCK) {
				cache = cipher.doFinal(data.getBytes(), offset, MAX_ENCRYPT_BLOCK);
			} else {
				cache = cipher.doFinal(data.getBytes(), offset, inputLen - offset);
			}
			out.write(cache, 0, cache.length);
			i++;
			offset = i * MAX_ENCRYPT_BLOCK;
		}
		byte[] encryptedData = out.toByteArray();
		out.close();
		// 获取加密内容使用base64进行编码,并以UTF-8为标准转化成字符串
		// 加密后的字符串
		return new String(Base64.getEncoder().encode(encryptedData));
	}

	/**
	 * RSA解密
	 *
	 * @param data 待解密数据
	 * @param privateKey 私钥
	 * @return
	 */
	public static String decrypt(String data, PrivateKey privateKey) throws Exception {
		Cipher cipher = Cipher.getInstance("RSA");
		cipher.init(Cipher.DECRYPT_MODE, privateKey);
		byte[] dataBytes = Base64.getDecoder().decode(data);
		int inputLen = dataBytes.length;
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		int offset = 0;
		byte[] cache;
		int i = 0;
		// 对数据分段解密
		while (inputLen - offset > 0) {
			if (inputLen - offset > MAX_DECRYPT_BLOCK) {
				cache = cipher.doFinal(dataBytes, offset, MAX_DECRYPT_BLOCK);
			} else {
				cache = cipher.doFinal(dataBytes, offset, inputLen - offset);
			}
			out.write(cache, 0, cache.length);
			i++;
			offset = i * MAX_DECRYPT_BLOCK;
		}
		byte[] decryptedData = out.toByteArray();
		out.close();
		// 解密后的内容
		return new String(decryptedData, "UTF-8");
	}

	/**
	 * 签名
	 *
	 * @param data 待签名数据
	 * @param privateKey 私钥
	 * @return 签名
	 */
	public static String sign(String data, PrivateKey privateKey) throws Exception {
		byte[] keyBytes = privateKey.getEncoded();
		PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
		KeyFactory keyFactory = KeyFactory.getInstance("RSA");
		PrivateKey key = keyFactory.generatePrivate(keySpec);
		Signature signature = Signature.getInstance("MD5withRSA");
		signature.initSign(key);
		signature.update(data.getBytes());
		return new String(Base64.getEncoder().encode(signature.sign()));
	}

	/**
	 * 验签
	 *
	 * @param srcData 原始字符串
	 * @param publicKey 公钥
	 * @param sign 签名
	 * @return 是否验签通过
	 */
	public static boolean verify(String srcData, PublicKey publicKey, String sign) throws Exception {
		byte[] keyBytes = publicKey.getEncoded();
		X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
		KeyFactory keyFactory = KeyFactory.getInstance("RSA");
		PublicKey key = keyFactory.generatePublic(keySpec);
		Signature signature = Signature.getInstance("MD5withRSA");
		signature.initVerify(key);
		signature.update(srcData.getBytes());
		return signature.verify(Base64.getDecoder().decode(sign.getBytes()));
	}


	public static void main(String[] args) throws Exception {
		initKey();
		String msg = "abcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfegfegabcdfegabcdfegabcdfegabcdfegabcdfeg";
		//公钥加密
		String enMsg = encryptByPublic(msg);
		//打印加密后的密文
		System.out.println(enMsg);
		//私钥解密
		String deMsg = decryptByPrivate(enMsg);
		//打印解密 后的明文
		System.out.println(deMsg);

	}
}
